//
//  AddMissionViewController.m
//  Four_years
//
//  Created by 中软国际088 on 15/11/10.
//  Copyright © 2015年 中软三班四年小组. All rights reserved.
//

#import "AddMissionViewController.h"
#import "SecondTableCell.h"
#import "ChooseClassifyCtr.h"
#import "SingleClassifyValue.h"
#import "FirstTableCell.h"
#import "DirayDB.h"
#import "DiraySingle.h"
@interface AddMissionViewController (){
    
    UITapGestureRecognizer *tap;
     UIImagePickerController * picker1;
    FirstTableCell * firstcell ;
    NSString *filePath ;
    
}
@property (nonatomic,strong) UITextField *eventText;
@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) UIDatePicker *datePic;
@property float dateCellHeight;
@property (nonatomic,strong) NSDate *chooseCurrent;
@end

@implementation AddMissionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //使相册不停顿
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
   
    
    [self initTable];
    
    [self initImagePick];
    
    [self initDatePickView];
    
    [self initGesture];
    
    
}

//初始化
- (void)initDatePickView{
   
    _datePic = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
    
    _datePic.datePickerMode =  UIDatePickerModeDate;
    
 
    
}

//初始化添加输入表
- (void)initTable{
    
    
    _table =  [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)style:UITableViewStyleGrouped];
    
    _table.dataSource = self;
    
    _table.delegate = self;
    
    [self.view addSubview:_table];
    
    
    
}

//设置表格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 2;
    }else
        return 3;
}

//设置表格分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}

//设置表格单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //返回第一个单元格
    if (indexPath.section==0 && indexPath.row == 0) {
        
      firstcell = [[FirstTableCell alloc]init];
        
        firstcell.selectionStyle = UITableViewCellSelectionStyleNone;
        firstcell.eventText.text = [DiraySingle shareManager].DirayNew.title;
        
        return firstcell;
        
        
    }
    //返回第一组第二个单元格
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        SecondTableCell *cell = [[SecondTableCell alloc]init];
        
//        passClassify =[SingleClassifyValue shareManager].classfiyName;
//        
//        //－－给单列日记类型赋值
//        diray.DirayNew.classify = passClassify;
        
        cell.lableName.text = @"分类";
        cell.lableKind.text = [DiraySingle shareManager].DirayNew.classify;
       
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
        
        
    }
    //第二组
    else  {
        SecondTableCell *cell = [[SecondTableCell alloc]init];
        
        //获得系统当前时间
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"M . d . YYYY"];
        NSString *date = [formatter stringFromDate:[DiraySingle shareManager].DirayNew.date];
        
        //创建日期选择器
        
        switch (indexPath.row) {
            case 0:
                
                cell.lableName.text = @"事件日期";
                cell.lableKind.text = date;
                return cell;
                
                break;
                
            case 1:
                
                     [cell.contentView addSubview:_datePic];
                
              
                
                return cell;
              
             case 2:
                
                
                iv.image = [UIImage imageNamed:filePath ];
                
                cell.backgroundView = v;
                cell.lableName.text = @"自定义背景";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                return cell;
                break;
            default:
                
                
                return cell;
                break;
        }
    }
    
}

//单元格触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
      
        ChooseClassifyCtr *chooseCtr = [[ChooseClassifyCtr alloc]init];
        
        switch (indexPath.row) {
            case 1:
                
                [self.navigationController pushViewController:chooseCtr animated:YES];
                
                break;
             
                
            default:
                break;
        }
        
    }
    else
    {
        switch (indexPath.row) {
            case 0:
                
                [self chooseDate];
                
                
                
                
                
                break;
            case 2:
                
   
                [self presentViewController:picker1 animated:YES completion:nil];
       
                
                break;
            default:
                break;
        }
    
    
    
    }
    
    
    
}

//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1 && indexPath.row ==1) {
        return _dateCellHeight;
    }
    else
    return 50;
    
}

//选择日期
- (void)chooseDate{

   
    
    if (_dateCellHeight == 0) {
        

        
       //展开下拉日期cell
        [UIView animateWithDuration:0.5 animations:^{
            
            _dateCellHeight = 150;
            
            

            
            [_table beginUpdates];
            [_table endUpdates];
            
            _datePic.frame = CGRectMake(0, 0, WIDTH, 150);
            
            
        }];

        
        
    }
    else{
    
        
        
        //收起下拉日期cell
        [UIView animateWithDuration:0.5 animations:^{
           
            _dateCellHeight = 0;
            
           //得到选择的日期
            [DiraySingle shareManager].DirayNew.date = _datePic.date;
            
            
            
           
            [_table beginUpdates];
            [_table endUpdates];
            
          _datePic.frame = CGRectMake(0, 0, 1, 1);
        
        }completion:^(BOOL finished) {
           
            [_table reloadData];
            
        } ];
  
          

        
       
    }
    
   


}

//创建选区本机照片
- (void)initImagePick{

    picker1 = [[UIImagePickerController alloc]init];
    picker1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker1.delegate = self;
    picker1.allowsEditing = YES;
   
}

//推出选择照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{
   
    //保存到沙盒中的图片的名字用当前系统的时间+.png来保存。保证图片不会重名被覆盖。
    NSDate *dateNow = [NSDate date];
    
    NSDateFormatter *form = [[NSDateFormatter alloc]init];
    
    [form setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *dateString = [form stringFromDate:dateNow];
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    NSString *path = [paths objectAtIndex:0];
  
   filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",dateString]];   // 保存文件的名称
    
                                                                                                                                                                                                     
    
    NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
    
   [data writeToFile: filePath atomically:YES];
  
    //－－给单列日记图片路径复制

    dateString = [NSString stringWithFormat:@"%@.png",dateString];
    
  
    [DiraySingle shareManager].DirayNew.imageName = dateString;
 
    [picker dismissViewControllerAnimated:YES completion:nil];


}
//初始化点击屏幕收起键盘的手势
- (void)initGesture{
    
    tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
    
    
}

//收回键盘手势
- (void)tap:(UITapGestureRecognizer *)sender{
    
    
    if (sender && [sender isKindOfClass:[UITapGestureRecognizer class]]) {
        
        [self.view endEditing:YES];
        
        
    }
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [_table reloadData];
    
    //注册键盘出现通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    //－－给单列日记标题赋值
    [DiraySingle shareManager].DirayNew.title = firstcell.eventText.text;
    
    
    //接触键盘通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];


       
}


//键盘打开触发事件
- (void)keyboardDidShow:(NSNotification *)notif{
    
    [self.view addGestureRecognizer:tap];
    
}

//键盘关闭触发事件
- (void)keyboardDidHide:(NSNotification *)notif{
    
    [self.view removeGestureRecognizer:tap];
}


@end
